package com.easyui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easyui.bean.Student;
import com.easyui.common.SelStudent;

public class ManageStudentServlet extends HttpServlet {
       
    public ManageStudentServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
		

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		JSONObject jsonAll = null;
		boolean success = false;
		String msg = "";
		
		//String tno = request.getParameter("tno");
		HttpSession session = request.getSession(); //获的session
		String role = (String)session.getAttribute("role");
		String tno = (String)session.getAttribute("userID");
		
System.out.println(role);
System.out.println(tno);

		if(role != null && role.trim().equals("teacher")) {//登录身份为老师
			if(tno != null && !tno.trim().equals("")) {//获取到该老师工号
				String ids = request.getParameter("ids");
				String id[] = ids.split(",");
				
				if(id.length > 0) {
					SelStudent ss = new SelStudent();
					success = ss.manageStudent(id, tno);
				} else {
					success = false;
				}
			} else {
				System.out.println("无法获取该教师工号");
				success = false;
			}
		} else {
			System.out.println("没有登录或者登录身份不是老师");
			success = false;
		}
		
		
		/*for(int i=0; i<id.length; i++) {
			System.out.println(id[i]);
		}*/
		
		if(success) {
			msg = "增加学生进行管理成功";
		} else {
			msg = "增加管理学生失败";
		}
		
		jsonAll= new JSONObject();
		jsonAll.put("success", success);
		jsonAll.put("msg", msg);
		
		out.print(jsonAll);
		out.flush();
		out.close();
	}

}
